<?php
namespace App\Service;
use App\Models\WebsitesAdmin;
use Illuminate\Support\Facades\Auth;

class CommonService
{
    public $adminId = "";
    public $parentId = 0;
    public $userType = 0;
    public function __construct()
    {
        $this->adminId = auth('admin')->id();
        if($this->adminId){
            $this->parentId = Auth::user()['parent_id'];
        }
    }

    /**
     * 获取登录用户的id
     * @return int|string|null
     */
    public static function getAdminId()
    {
        return auth('admin')->id();
    }

    /**
     * 获取账号所属的主账号id
     * @return mixed
     */
    public static function getParentId()
    {
        return Auth::user()['parent_id'];
    }

    /**
     * 账号权限
     * @return void
     */
    public static function accountAuthType()
    {
         $adminId = Auth::user()['id'];
         $parentId = Auth::user()['parent_id'];
         if($adminId == 1 || $parentId ==1){
             $userType = 1;//超级管理员
         }elseif (!$parentId){
             $userType = 2;//一级主账号
         }else{
             $userType = 3;//普通的子账号
         }
         return $userType;
    }

    /**
     * 得到网站主账号id
     * @return void
     */
    public static function masterAccountId()
    {
        $adminId = self::getAdminId();
        $parentId = self::getParentId();
        if ($parentId) {
            return $parentId;
        }
        return $adminId;
    }


    /**
     * 获取网站操作人信息
     * @param $websiteId
     * @return array
     */
    public static function operationData($websiteId='')
    {
        $result['admin_id'] = 0;
        $result['operation_id'] = 0;
        if(!$websiteId){
            return $result;
        }
        $getAdminId = self::getAdminId();
        $websiteAdmin = WebsitesAdmin::query()->where('website_id',$websiteId)->select('id','admin_id','website_id')->first();
        if(!$websiteAdmin){//不存在的情况
            $result['admin_id']   = $getAdminId;
            $result['operation_id'] = $getAdminId;
            return $result;
        }
        if($websiteAdmin['admin_id'] == $getAdminId){//本人操作
            $result['admin_id']   = $getAdminId;
            $result['operation_id'] = $getAdminId;
        }else{//主账号操作
            $result['admin_id']   = $websiteAdmin['admin_id'];
            $result['operation_id'] = $getAdminId;
        }
        return $result;
    }

    /**
     * 成功回调
     * @param string $msg 提示信息
     * @param array $data 返回数据
     * @param int $code 提示码
     * @param int $count 总数据量
     * @return mixed
     */
    protected function success(string $msg = "success",  int $count = 0,array $data = [], $code = 0)
    {
        $msg =  ['code' => $code, 'msg' => $msg, 'data' => $data,'count'=>$count];
        return  $msg;
    }

    /**
     * 失败回调
     * @param string $msg 提示信息
     * @param array $data 返回数据
     * @param int $code 提示码
     * @return mixed
     */
    protected function error(string $msg = 'error', array $data = [], $code = 1)
    {
        $msg =  ['code' => $code, 'msg' => $msg, 'data' => $data];
        return  $msg;
    }
}
